<?php
//
// +------------------------------------------------------------------------+
// | PHP Version 5.4                                                        |
// +------------------------------------------------------------------------+
// | Copyright (c) All rights reserved.                                     |
// +------------------------------------------------------------------------+
// | 世纪车来车往(北京）网络科技有限公司 版权所有！                         |
// | TEL:010-63732365  HOTLINE:400-6688-365  EMAIL:clcw@clcw.com.cn         |
// | $Revision: 1.0 $                                                       |
// +------------------------------------------------------------------------+
// | Author: songaimin@clcw.com.cn  Blog:http://blog.csdn.net/samxx8        |
// +------------------------------------------------------------------------+
//
namespace AuctionCore\Common;

require_once dirname(__FILE__) . '/../../Common/Lib/Aliyun/OSS/sdk.class.php';

/**
 * 图片处理类
 * @author       Eric Lee
 */
class Image {

	const endpoint = OSS_ENDPOINT;
	const accessKeyId = OSS_ACCESS_ID;
	const accesKeySecret = OSS_ACCESS_KEY;

	function __construct() {

	}

	public static function get_oss_client() {
		$oss = new \ALIOSS(self::accessKeyId, self::accesKeySecret, self::endpoint);
		return $oss;
	}

	/*
		     * 文件上传
		     * @param $bucket string
		     * @param $objcet string
		     * @param $content string 文件内容
		     * @param $prefix string 文件名前缀
		     * @return string 文件绝对地址
	*/
	public static function upload_file_by_content($bucket, $object, $content, $prefix = '') {
		$oss = self::get_oss_client();
		if (!$content) {
			throw new \Exception('文件内容不能为空');
		}

		$pathinfo = pathinfo($object);
		$object = ($pathinfo['dirname'] == '.' ? '' : $pathinfo['dirname'] . '/') . self::create_uuid($prefix) . '.' . $pathinfo['extension'];

		$options = array(
			'content' => $content,
			'length' => strlen($content),

			\ALIOSS::OSS_HEADERS => array(
				'Expires' => 'Fri, 28 Feb 2012 05:38:42 GMT',
				'Cache-Control' => 'no-cache',
				'Content-Disposition' => 'attachment;filename=' . $object,
				'Content-Encoding' => 'utf-8',
				'Content-Language' => 'zh-CN',
				'x-oss-server-side-encryption' => 'AES256',
			),
		);

		try {
			$res = $oss->upload_file_by_content($bucket, $object, $options);
		} catch (\Exception $e) {
			return $e->getMessage();
		}

		if ($res->isOk()) {
			//return "http://{$bucket}.oss-cn-beijing.aliyuncs.com/{$object}";
			return "{$object}";
		} else {
			return '';
		}
	}

	/*
		     * 文件上传
		     * @param $bucket string
		     * @param $objcet string 保存在阿里云服务器上的文件名称
		     * @param $file_path string 要上传的本地文件路径
		     * @param $prefix string 文件名前缀
		     * @param $type 图片类型 car 车图片，head 头像
	         * @param $customPath 是否自定义路径
		     * @return string 文件绝对地址
	*/
	public static function upload_file_by_file($bucket, $object, $file_path, $options = null, $prefix = '', $type = 'car' ,$customPath = false) {
		$oss = self::get_oss_client();
		if($customPath){
			$object = $type . '/' . basename($file_path);
		}else{
			$object = $type . '/' . self::get_object_prefix($file_path) . self::create_uuid($prefix) . '.' . get_extension($object);
		}
		try {
			$res = $oss->upload_file_by_file($bucket, $object, $file_path, $options);
		} catch (\Exception $e) {
			throw new \Exception($e->getMessage());
		}

		if ($res->isOk()) {
			//return "http://{$bucket}.oss-cn-beijing.aliyuncs.com/{$object}";
			return "{$object}";
		} else {
			return '';
		}
	}
	//应用包上传
	public static function upload_package_by_file($bucket, $object, $file_path, $options = null, $prefix = '', $type = 'app'){
		$oss = self::get_oss_client();
		$object = $type . '/' . $object;
		try {
			$res = $oss->upload_file_by_file($bucket, $object, $file_path, $options);
		} catch (\Exception $e) {
			throw new \Exception($e->getMessage());
		}
		if ($res->isOk()) {
			return "{$object}";
		} else {
			return '';
		}
	}
	/**
	 * 检查重复，根据图片二进制hash算法，检查是否重复
	 * @param $tmp_file 上传文件时的临时文件
	 * @return   String 将保存在阿里云服务器上的文件名相对路径(bucket 下面的路径)
	 */
	public function get_object_prefix($file) {
		$file_md5 = md5($file);
		$dir1 = substr($file_md5, 0, 4);
		$dir2 = substr($file_md5, 4, 4);
		return date('Y-m-d') . '/' . $dir1 . '/' . $dir2 . '/';
	}

	//生成唯一ID
	function create_uuid($prefix = "") {
		//可以指定前缀
		$str = md5(uniqid(mt_rand(), true));
		$uuid = substr($str, 0, 8) . '-';
		$uuid .= substr($str, 8, 4) . '-';
		$uuid .= substr($str, 12, 4) . '-';
		$uuid .= substr($str, 16, 4) . '-';
		$uuid .= substr($str, 20, 12);
		return $prefix . $uuid;
	}

	//获得图像的完整url地址
	public static function get_full_url($img_path){
		if(!$img_path)
			return '';
		$s4 = substr($img_path, 0,4);
		if($s4!='http'){
			$prefixs = C('CONTENT_IMG_PREFIX');
			$n = rand(0,3);
			$prefix = $prefixs[$n];
			$s1 = substr($img_path, 0,1);
			if($s1=='/'){
				$img_path = $prefix.$img_path;
			}else{
				$img_path = $prefix.'/'.$img_path;
			}
		}
		return $img_path;
	}
}

?>